﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace TextEditor.TextBoxInner.Utils
{
	internal static class FontHelper
	{
		internal const double FontSize = 14;

		internal static readonly double FontHeight;
		internal static readonly double FontWidth;
		internal static readonly Typeface Typeface;

		internal static readonly GlyphTypeface GlyphTypeface;
		internal static readonly ushort GlyphSpaceIndex;
		internal static readonly ushort GlyphUnknownIndex;
		internal static readonly List<ushort> GlyphIndexes = new List<ushort>(100);
		internal static readonly List<double> AdvanceWidths = new List<double>(100);

		internal static readonly Brush DefaultFontBrush = Brushes.Black;

		static FontHelper()
		{
			Typeface = new Typeface(new FontFamily("Consolas"), Application.Current.MainWindow.FontStyle, Application.Current.MainWindow.FontWeight, Application.Current.MainWindow.FontStretch);
			FormattedText testText = new FormattedText("X", CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, Typeface, FontSize, Application.Current.MainWindow.Foreground, new NumberSubstitution(), TextFormattingMode.Display);

			FontHeight = testText.Height;
			FontWidth = testText.Width;

			// initialize glyph
			if (!Typeface.TryGetGlyphTypeface(out GlyphTypeface))
				throw new InvalidOperationException("No glyphtypeface found");

			GlyphSpaceIndex = GlyphTypeface.CharacterToGlyphMap[' '];
			GlyphUnknownIndex = GlyphTypeface.CharacterToGlyphMap[0x2666];
			//
		}
	}
}
